package cn.valo.org.controller;

import cn.valo.org.domain.Employee;
import cn.valo.org.query.EmployeeQuery;
import cn.valo.org.service.IEmployeeService;
import cn.valo.basic.util.AjaxResult;
import cn.valo.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService iEmployeeService;
    //id查询
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return iEmployeeService.queryById(id);
    }
    //查所有
    @GetMapping()
    public List<Employee> queryAll(){
        return iEmployeeService.queryAll();
    }
    //高级查询+分页
    @PostMapping
    public PageList<Employee> list(@RequestBody EmployeeQuery query){
        System.out.println("-------------");
        System.out.println(query);
        return iEmployeeService.queryPage(query);
    }

    @DeleteMapping("/{id}") // /employee/1
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            iEmployeeService.removeById(id);

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @PutMapping //添加或修改
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {

        try {
            if (employee.getId() != null) {
                iEmployeeService.update(employee);
            } else {
                iEmployeeService.save(employee);
            }

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

    }
}
